using System;
using System.ComponentModel;
using System.Globalization;

namespace PickGold.Charting
{
	internal class DataPointValueConverter : DoubleConverter
	{
		public override object ConvertFrom(ITypeDescriptorContext context, CultureInfo culture, object value)
		{
			if ((context != null) && (context.Instance != null))
			{
				string s = value as string;
				if (s != null)
				{
					DataPoint instance = (DataPoint)context.Instance;
					if (instance.series.IsXValueDateTime())
					{
						return DateTime.Parse(s, CultureInfo.CurrentCulture).ToOADate();
					}
				}
			}
			return base.ConvertFrom(context, culture, value);
		}

		public override object ConvertTo(ITypeDescriptorContext context, CultureInfo culture, object value, Type destinationType)
		{
			if ((context != null) && (context.Instance != null))
			{
				DataPoint instance = (DataPoint)context.Instance;
				if ((destinationType == typeof(string)) && instance.series.IsXValueDateTime())
				{
					return DateTime.FromOADate((double)value).ToString("g", CultureInfo.CurrentCulture);
				}
			}
			return base.ConvertTo(context, culture, value, destinationType);
		}
	}
}

